<template>
    <div>
        <Modal v-model="showDataModalFlag" title="修改QA" @on-ok="handleSubmit2('formValidate2')" :mask-closable="false">
            <Form :label-width="100" ref="formValidate2" :rules="ruleValidate2" :model="data2">
                <FormItem label="问题" prop="year">
                    <Input placeholder="问题" class="input-select-class" v-model="data2.question"></Input>
                </FormItem>
                <FormItem label="答案" prop="operate_name2">
                    <Input placeholder="答案" class="input-select-class" v-model="data2.answer"></Input>
                </FormItem>
                <FormItem label="描述" prop="operate_name2">
                    <Input placeholder="描述" class="input-select-class" v-model="data2.described"></Input>
                </FormItem>
                <FormItem label="类别">
                    <Select style="width:100%" v-model="data2.type">
                        <Option v-for="target in qatypelist" :value="target.name">{{target.name}}</Option>
                    </Select>
                </FormItem>
                <FormItem label="排序" prop="operate_name2">
                    <Input placeholder="排序" class="input-select-class" v-model="data2.order"></Input>
                </FormItem>
            </Form>
        </Modal>
        <Row style="margin-top:20px">
            <Col span="19" class="table-form-input-select-col">
            <span class="alltitle"><router-link to="项目列表"> 项目列表</router-link>>Q&A</span>
            </Col>
        </Row>
        <Row class="fazhanlicheng" style="margin-top:20px;height:40px;padding-left:20px">
            <span class="title">Q&A</span>
        </Row>
        <Row class="fazhanlicheng" style="heigh:60px;">
          <Row>
            <Col class="inner-info2" offset="1">问题</Col>
            <Col span="5">
            <Input class="time-picker" placeholder="输入问题" style="width: 100%" v-model="question" />
            </Col>
            <Col class="inner-info2" offset="1">答案</Col>
            <Col span="6">
            <Input class="time-picker" placeholder="输入答案" style="width: 100%" v-model="answer" />
            </Col>
            <Col class="inner-info2" offset="1">描述</Col>
            <Col span="6">
            <Input class="time-picker" placeholder="输入描述" style="width: 100%" v-model="described" />
            </Col>
          </Row>
          <Row>
            <Col class="inner-info2" offset="1">类别</Col>
            <Col span="5">
              <Select class="time-picker" style="margin-right:10px;width:100px" v-model="qatype">
                  <Option v-for="target in qatypelist" :value="target.name">{{target.name}}</Option>
              </Select>
              <Button type="primary" style="margin-top:14px;margin-right:10px;width:50px" @click="addqatype">添加</Button>
              <Button type="error" style="margin-top:14px;width:50px" @click="delqatype">删除</Button>
            </Col>
            <Col class="inner-info2" offset="1">排序</Col>
            <Col span="2">
              <Input class="time-picker" placeholder="排序数字" style="width: 100%" v-model="sort" />
            </Col>
          </Row>
        </Row>
        <Row class="add-new-progress" style="heigh:60px;background:#f5f7f9">
            <Button type="primary" style="width:100px" @click="handleCoInfo">确认提交</Button>
        </Row>
        <div class="content-div" style="margin-top:10px">
            <Table border stripe :columns="columns2" :data="data" ref="table"></Table>
        </div>
    </div>
</template>
<script>
import {
    formatDate
} from '@/js/date.js'
export default {
    data() {
            return {
                formValidate2: {

                },
                ruleValidate2: {

                },
                data: [],
                columns2: [{
                    title: "序号",
                    key: "id",
                    width: 99,

                }, {
                    title: "问题",
                    key: "question",

                }, {
                    title: "答案",
                    key: "answer",

                }, {
                    title: "类别",
                    key: "type",
                    width: 150,

                }, {
                    title: "描述",
                    key: "described",
                    width: 150,

                }, {
                    title: "排序",
                    key: "order",
                    width: 150,
                    sortType:"asc",

                }, {
                    title: "操作",
                    key: "action",
                    width: 99,

                    render: (h, params) => {
                        return h("div", [
                            h(
                                "span", {
                                    style: {
                                        textDecoration: "underline",
                                        color: "#4b80fd",
                                        cursor: "pointer"
                                    },
                                    on: {
                                        click: () => {
                                            this.edit(params.index);
                                        }
                                    }
                                },
                                "编辑"
                            ),
                            h(
                                "span", {
                                    style: {
                                        textDecoration: "underline",
                                        color: "#4b80fd",
                                        cursor: "pointer",
                                        'margin-left': "5px",
                                    },
                                    on: {
                                        click: () => {
                                            this.removeData(params.index);
                                        }
                                    }
                                },
                                "删除"
                            )
                        ], );
                    }
                }],
                showDataModalObject: {},
                data2: {},
                showDataModalFlag: false,
                question: '',
                answer: '',
                described: '',
                qatype: '',
                qatypelist: [],
                sort: ''
            }
        },
        methods: {
            addqatype() {
                this.$Modal.confirm({
                    render: (h) => {
                        return h('Input', {
                            props: {
                                value: this.value,
                                autofocus: true,
                                placeholder: '请输入类别'
                            },
                            on: {
                                input: (val) => {
                                    this.value = val;

                                }
                            }
                        })
                    },
                    onOk: () => {
                        var self = this
                        var poid = localStorage.pid;

                        $.ajax({
                            url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=qatypecreate&id=" + poid,
                            data: {
                                'name': this.value
                            },
                            type: "POST",
                            dataType: "json",
                            success: function(result) {
                                console.log(result);
                                if (result.code == 1) {
                                    self.$Message.success('添加成功！');
                                    self.qatypesearch();

                                } else {
                                    self.$Message.error('添加失败');
                                }
                            }
                        });

                    }
                })
            },
            delqatype() {
                var self = this
                var poid = localStorage.pid;

                self.$Modal.confirm({
                    title: '确认删除？',
                    content: '',
                    onOk: () => {
                        $.ajax({
                            url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=qatypedel&id=" + poid + "&name=" + self.qatype,

                            type: "POST",
                            dataType: "json",
                            success: function(data) {
                                self.qatypesearch();
                                console.log(data);

                            }
                        });
                    },
                    onCancel: () => {
                        this.$Message.info('取消删除');
                    }
                });
            },
            removeData(index) {
                var self = this
                self.$Modal.confirm({
                    title: '确认删除？',
                    content: '',
                    onOk: () => {

                        var self = this;
                        self.$api.post('/api/v1/edit', self.data[index], function(result) {
                            self.data.splice(index, 1)
                            self.$Message.info(result.message)
                        })


                        $.ajax({
                            url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=qadel&id=" + self.data[index].id,

                            type: "POST",
                            dataType: "json",
                            success: function(data) {

                                console.log(data);

                            }
                        });
                    },
                    onCancel: () => {
                        this.$Message.info('取消删除');
                    }
                });

            },
            edit(index) {
                this.showDataModalFlag = true;
                this.data2 = this.data[index];

                console.log(this.data[index])
            },


            handleCoInfo() {

                var poid = localStorage.pid;
                var self = this

                $.ajax({
                    url: " https://evp.51job.com/51job/api/51miniadmin/back.php?action=qacreate&id=" + poid,
                    data: {
                        question: self.question,
                        answer: self.answer,
                        type: self.qatype,
                        described: self.described,
                        order:self.sort
                    },
                    type: "POST",
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        self.$Message.success("添加成功！");
                        self.qalist();

                    }
                });
            },
            handleSubmit2() {
                var self = this
                    /* self.time2[0] = formatDate(new Date(self.time2[0]), 'yyyy-MM-dd');
                     self.time2[1] = formatDate(new Date(self.time2[1]), 'yyyy-MM-dd')*/
                $.ajax({
                    url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=qaedit&id=" + self.data2.id,
                    data: {
                        question: self.data2.question,
                        answer: self.data2.answer,
                        type: self.data2.type,
                        described: self.data2.described,
                        order:self.data2.order

                    },
                    type: "POST",
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        self.$Message.success("修改成功！");
                    }
                });
            },
            qatypesearch() {
                var self = this;
                var poid = localStorage.pid;
                $.ajax({
                    url: " https://evp.51job.com/51job/api/51miniadmin/back.php?action=qatypelist&id=" + poid,
                    type: "POST",
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        self.qatypelist = data.data;


                    }
                });
            },
            qalist() {
                var self = this;
                var poid = localStorage.pid;

                $.ajax({
                    url: " https://evp.51job.com/51job/api/51miniadmin/back.php?action=qalist&id=" + poid,
                    type: "POST",
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        self.data = data.data;


                    }
                });
            },


        },
        created() {
            var self = this;
            var poid = localStorage.pid;
            self.qatypesearch();
            self.qalist();
        }
}
</script>
<style scoped>
.innertitle {
    line-height: 30px;
    font-size: 14px;
    text-decoration: underline;
    color: #4B80FD;
    font-family: MicrosoftYaHei;
    cursor: pointer;
}

.fazhanlicheng {
    box-sizing: border-box;
    background: #fff;
    margin-bottom: 1px;
    padding: 0px;
}

.title {
    font-size: 14px;
    color: #333333;
    letter-spacing: 0;
    line-height: 40px;
    font-weight: bold;
}

.inner-info2 {
    float: left;
    line-height: 60px;
    font-size: 14px;
    color: #333;
    font-family: MicrosoftYaHei;
    margin-right: 10px;
}

.time-picker {
    margin-top: 15px;
}

.add-new-progress {
    box-sizing: border-box;
    background: #fff;
    padding: 0;
    text-align: center;
    color: #4B80FD;
    font-family: SourceHanSansCN-Normal;
    font-size: 14px;
    line-height: 60px;
    cursor: pointer;
}
</style>
